package com.example.xd.default_call

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import android.telecom.TelecomManager
import androidx.core.content.ContextCompat

/**
 * @auther XuDan on 2020/8/13.
 */
object DefaultCallUtil {
    /**
     * 判断是否拥有通话权限
     */
    fun hasPermission(context: Context, permissions: Array<String>): Boolean {
        for (i in 0..permissions.size - 1) {
            if (ContextCompat.checkSelfPermission(context, permissions.get(i))
                != PackageManager.PERMISSION_GRANTED
            ) {
                return false
            }
        }
        return true
    }

    /**
     * 判断是否为默认通话
     */
    fun isDefaultCall(context: Context): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val mTelecom: TelecomManager =
                context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
            val packageName = context.packageName
            if (mTelecom.defaultDialerPackage.equals(packageName)) {
                return true
            } else {
                return false
            }
        }
        return false
    }

    fun isFloating(context: Context): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(context)) {
                return true
            } else {
                return false
            }
        } else {
            return true
        }
    }
}